<template>
	<view :class="cancel?'mask':''">
		<view v-for="item in detaillist" :key="item._id">
			<view>
				<block>
					<view class="order_tit">
						<text class="item.icon"></text>
						<text class="order_yy"></text>
					</view>
				</block>
			</view>
			<view class="order_addr">
				<text class="iconfont icon-ditu_dingwei"></text>
				<view class="order_addrBox">
					<text class="order_name">
						收货人：{{item.address.name}}
						<text>{{item.address.tel}}</text>
					</text>
					<text class="order_dizhi">{{item.address.addr_text}} {{item.address.detail}}</text>
				</view>
			</view>
			<view class="order_good" v-for="itemo in item.goods" :key="itemo.goods_id" @click="product(itemo.goods_id)">
				<block>
					<view class="order_list">
						<view class="info">
							<image :src="itemo.img"></image>
							<view class="order_goodBox">
								<view class="order_goodName">{{itemo.name}}</view>
								<view class="order_goodAttr">{{itemo.attr}}</view>
								<view class="order_price">
									<text class="order_jq">¥{{itemo.price}}</text>
									<text class="order_num">x{{itemo.num}}</text>
								</view>
							</view>
						</view>
					</view>
					<view class="order_btn">
						<!-- 	<text :data-img="item.thumb" :data-id="item._id" :data-name="item.title" :data-attr="item.attr"
						 :data-price="item.price" :data-num="item.num" @click="tuikuan" v-if="item.status == 2">退款</text> -->
						<text class="act" v-if="item.status== 6 || item.status == 7">评价</text>
					</view>
				</block>
			</view>
			<view class="order_cont">
				<view class="order_contBox">
					<text class="order_contPa">商品总额</text>
					<text class="order_contPb">¥{{item.all_amount}}</text>
				</view>
				<view class="order_contFinal">
					<text class="order_contPa">应付金额</text>
					<text class="order_contPb">¥{{item.base_money}}</text>
				</view>
			</view>
			<view class="order_main">
				<text class="order_mainTit">订单信息</text>
				<view class="order_mainBox">
					<text class="order_mainPa">下单时间</text>
					<text class="order_mainPb"> {{xiadantime}}</text>
				</view>
				<view class="order_mainBox">
					<text class="order_mainPa">订单编号</text>
					<text class="order_mainPb"> {{item.order_sn}}</text>
				</view>
				<view class="order_mainBox" v-if="!cancel_text">
					<text class="order_mainPa">付款金额</text>
					<text class="order_mainPb">  {{item.base_money}}元</text>
				</view>
				<view class="order_mainBox" v-if="!cancel_text">
					<text class="order_mainPa">支付方式</text>
					<text class="order_mainPb" v-if="item.pay_type=='alipay'">支付宝</text>
					<text class="order_mainPb" v-else="item.pay_type=='wxpay'">微信</text>
				</view>
				<view class="order_mainBox" v-if="!cancel_text">
					<text class="order_mainPa">支付时间</text>
					
					<text class="order_mainPb" v-if="item.status==8">{{item.cancel_text}}</text>
					<text class="order_mainPb" v-else-if="item.status==1">待付款</text>
					<!-- <text class="order_mainPb" v-else>{{item.update_time}}</text> -->
					<text class="order_mainPb" v-else>{{up_time}}</text>
				</view>
				<view class="order_mainBox" v-if="!cancel_text">
					<text class="order_mainPa">物流公司</text>
					<text class="order_mainPb" v-if="express_com=='HTKY'">百世快递</text>
					<text class="order_mainPb" v-if="express_com=='STO'">申通快递</text>
					<text class="order_mainPb" v-if="express_com=='YTO'">圆通速递</text>
					<text class="order_mainPb" v-if="express_com=='HHTT'">天天快递</text>
				</view>
				<view class="order_mainBox" v-if="!cancel_text">
					<text class="order_mainPa">物流单号 </text>
					<text class="order_mainPb">{{express_no}}</text>
				</view>
				<view class="order_mainBox">
					<text class="order_mainPa">订单备注 </text>
					<text class="order_mainPb" v-if="item.desc"> {{item.desc}}</text>
					<text class="order_mainPb" v-if="!item.desc"> 无备注</text>
				</view>
				<view class="order_mainBox">
					<text class="order_mainPa">订单状态 </text>
					<text class="order_mainPb"> {{order_text}}</text>
				</view>
				<view class="order_mainBox" v-if="cancel_text">
					<text class="order_mainPa">取消原因 </text>
					<text class="order_mainPb"> {{cancel_text}}</text>
				</view>
			</view>
			
			<view class="order_finals">
				<!-- 待付款 -->
				<view class="order_finalsRight" v-if="item.status==1">
					<text @click="cancel=true">取消订单</text>
					<text class="act" @click="go_pay(item._id,item.base_money)">去付款</text>
				</view>
				
				<!-- 待发货2 待收货3 -->
				<view class="order_finalsRight" v-else-if="item.status==2 || item.status==3">
					<view style="position: fixed;left: 15px;bottom: 8px;border: 1px solid #f00;padding: 5px 8px;" v-if="item.status==2" @click="send">发货</view>
					<text class="act" @click="wuliu(item._id)">查看物流</text>
					<text class="act" @click="shouhuo(item._id)">确认收货</text>
				</view>
				
				<!-- 已取消 -->
				<view class="order_finalsRight" v-else="item.status==8" @click="del_order(item._id)">
					<text>删除订单</text>
				</view>
			
				<!-- 已完成 -->
				<view class="order_finalsLeft" v-else-if="item.status== 6 || item.status == 7">
					<view class="order_finalBox">
						<text class="iconfont icon-dianhua"></text>
						<text class="order_finalPa">联系客服</text>
					</view>
				</view>
			</view>
				
<!-- ###################### -->
			
			<!-- 取消订单 原因填写 -->
			<view class=""  v-if="cancel">
				<!-- 遮罩层 -->
				<view class="order-meanu">
					<!-- 取消原因的输入框 -->
					<view class="order_cancel">
						<view class="tui-popup-box">
							<view>请您填写一下取消原因：</view>
							<input type="text" placeholder="请输入" v-model="inp_cancel"/><!-- bug  -->
							<view class="button_wrap">
								<button type="default" @click="cancel=false">取消</button>
								<button type="primary" @click="cancelOrder">确定</button>
							</view>
						</view>
					</view>
				</view>
			</view>
			
			
		
		</view>
	</view>
</template>

<script>
	import { timeForm } from '@/utils/TimeFrom.js'; //时间戳转为日期
	export default {
		data() {
			return {
				order_id:'',//订单id
				detaillist:'',//订单详情
				cancel:false,//是否取消 的遮罩层
				cancel_text:"",//取消原因
				order_text:'',//订单状态
				inp_cancel:'',//用户输入的取消订单的原因
				xiadantime:'',//下单时间
				up_time:'',//支付时间
				now: new Date().getTime(), // 现在的时间戳
				express_com:'',//物流公司"HTKY": "百世快递", "STO": "申通快递", "YTO": "圆通速递","HHTT": "天天快递"
				express_no:'',//物流单号
				host: 'https://4de16698-d9f4-443a-b416-ef1037cce952.bspapp.com/http'
			};
		},
		onLoad(options) {
			console.log(options);
			this.order_id=options.order_id;
			// order_text订单状态
			// cancel_text取消原因
			this.order_text=options.order_text;
			if(options.cancel_text=='undefined'){
				this.cancel_text=''
			}else{
				this.cancel_text=options.cancel_text;
			}
		},
		onShow() {
			this.orderdetail();
		},
		mounted() {
			// this.orderdetail();
		},
		methods:{
			// 订单详情
			orderdetail(){
				let token = uni.getStorageSync('token');
				this.token = token;
				let uid = uni.getStorageSync('uid');
				this.uid = uid;
				uni.request({
					method: 'POST',
					header: {
						'token': this.token
					},
					data: {
						uid:this.uid,
						orderId:this.order_id
					},
					url: this.host + '/order/getOrderDetail',
					success: res => {
						console.log(res);
						this.detaillist=res.data.data
						this.detaillist = this.detaillist.map((v)=>{
							return{//v当前元素的值
								...v,//当前元素的所有的值
								cancel_text:this.cancel_text
							}
						})//map() 方法返回一个新数组，数组中的元素为原始数组元素调用函数处理后的值
						console.log(this.detaillist);
						this.express_com=res.data.data[0].express_com
						this.express_no=res.data.data[0].express_no
						
						this.xiadantime = timeForm(this.detaillist[0].add_time, false, this.now);
						this.up_time = timeForm(this.detaillist[0].update_time, false, this.now);
						this.cancel_text=this.detaillist[0].cancel_text
						// console.log(this.detaillist[0].cancel_text);
					},
					fail: () => {
						console.log('请求失败');
					}
				});
			},
			// 取消订单
			cancelOrder(){
				if(this.inp_cancel==''){
					uni.$u.toast('请填写取消原因')
				}else{
					uni.request({
						method: 'POST',
						header: {
							'token': this.token
						},
						data: {
							uid:this.uid,
							orderId:this.order_id,
							cancel_text:this.inp_cancel//取消原因
						},
						url: this.host + '/order/cancel_order',
						success: res => {
							// order_text订单状态
							// cancel_text取消原因
							// inp_cancel用户输入的取消订单的原因
							this.cancel_text=this.inp_cancel
							this.cancel=false
							this.order_text='已取消'
							uni.$u.toast(res.data.msg)
							this.orderdetail();
						}
					});
				}
			},
			// 删除订单
			del_order(o_id){
				let token = uni.getStorageSync('token');
				this.token = token;
				console.log(o_id);
				uni.request({
					method: 'POST',
					header: {
						'token': this.token
					},
					data: {
						orderId: o_id
					},
					url: this.host + '/order/del_order',
					success: res => {
						console.log(res);
						uni.$u.toast('删除成功')
						uni.navigateTo({
							url: '/pages/order/order'
						})
					},
					fail: () => {
						console.log('请求失败');
					}
				});
			},
			// 去付款
			go_pay(pay_id,base_money){
				console.log(base_money);
				uni.navigateTo({
					url:`/pages/money/pay?order_id=${pay_id}&money=${base_money}`
				})
			},
			// 发货 此操作为开发时使用 操作完删掉
			send(){
				let token = uni.getStorageSync('token');
				this.token = token;
				uni.request({
					method: 'POST',
					header: {
						'token': this.token
					},
					data: {
						orderId:this.order_id,
						express_com:'YTO',//快递公司编码"HTKY": "百世快递", "STO": "申通快递", "YTO": "圆通速递","HHTT": "天天快递"
						// express_com:this.express_com,
						express_no:'YT8618320512291' ,//快递号
						// ZTO
						// 78325603586025
						// express_no:this.express_no
					},
					url: this.host + '/order/send',
					success: res => {
						console.log(res);
						if(res.data.code==1){
							uni.$u.toast('发货成功')
						}
					},
					fail: () => {
						console.log('请求失败');
					}
				});
			},
			// 查看物流
			wuliu(o_id){
				uni.navigateTo({
					url:`/pages/order/wuliu?order_id=${o_id}`
				})
			},
			// 确认收货
			shouhuo(o_id){
				console.log(o_id);
				uni.request({
					method: 'POST',
					data: {
						orderId: o_id
					},
					url: this.host + '/order/shouhuo',
					success: res => {
						console.log(res);
					},
					fail: () => {
						console.log('请求失败');
					}
				});
			},
			// 跳转到商品详情页
			product(goods_id){
				uni.navigateTo({
					url: '/pages/product/product?goods_id='+goods_id,
				})
			}
		}
	}
</script>

<style lang="scss">
.mask{
	// 遮罩层出来后 禁止遮罩层后的内容滚动
	overflow: hidden;
	position:fixed;
	height: 100%;
	width: 100%;
}
.order-meanu{
	background-color: rgba(0, 0, 0, 0.5);
	width: 100%;
	height: 100vh;
	position: fixed;
	z-index: 99;
	top: 0;
	left: 0;
}
.order_cancel{
	width: 100%;
	position: absolute;
	bottom: 0;
	left: 0;
	background-color: #fff;
	z-index: 99;
	input{
		padding-left: 10px;
	}
}
.popover_tit {
	width: 100%;
	height: auto;
	overflow: hidden;
	position: relative;

	&:before {
		content: '';
		position: absolute;
		left: 30upx;
		right: 30upx;
		bottom: 0;
		background: #eeeeee;
		transform: scaleY(0.5);
		height: 1px;
	}

	.popover_bt {
		font-size: 32upx;
		font-weight: 500;
		line-height: 90upx;
		display: block;
		text-align: center;
	}

	.iconfont {
		position: absolute;
		right: 30upx;
		top: 0;
		bottom: 0;
		margin: auto;
		color: #666;
		font-size: 42upx;
		line-height: 90upx;
	}
}

.tui-popup-scroll {
	max-height: 342upx;
}

.popover_main {
	width: 100%;
	height: auto;
	overflow: hidden;
	padding: 0 30upx;

	.popover_choose {
		width: 100%;
		height: auto;
		overflow: hidden;
		border-bottom: 1px #eeeeee solid;

		.iconfont {
			font-size: 36upx;
			line-height: 100upx;
			display: block;
			float: left;
			width: 36upx;
			height: 36upx;
		}

		.popover_reason {
			display: block;
			float: left;
			margin-left: 24upx;
			font-size: 30upx;
			color: #000000;
			line-height: 100upx;
		}
	}

	.act .iconfont:before {
		content: '\E655';
		color: $color1;
	}
}

.popover_btn {
	width: 100%;
	height: 100upx;
	background: white;
	border: 1px solid #f12435;
	color: #f12435;
	margin-top: 30upx;
	font-size: 32upx;
	// color: white;
	text-align: center;
	line-height: 100upx;
}

.order_tui {
	width: 100%;
	height: auto;
	background: white;
	overflow: hidden;

	.order_tuiTit {
		font-size: 30upx;
		border-bottom: 1px #eeeeee solid;
		font-weight: bold;
		line-height: 100upx;
		padding: 0 30upx;
		display: block;
		overflow: hidden;
	}

	.order_tuiBox {
		width: 100%;
		height: auto;
		overflow: hidden;
		padding: 0 30upx;
		border-bottom: 1px solid rgba(238, 238, 238, 1);

		.order_tuiPa {
			font-size: 30upx;
			color: #333333;
			line-height: 100upx;
			float: left;
			display: block;
			width: 160upx;
		}

		.order_tuiPb {
			font-size: 30upx;
			color: #333333;
			line-height: 100upx;
			float: left;
			display: block;
			width: calc(100% - 160upx);
		}

		.order_tuiImgBox {
			width: auto;
			height: auto;
			float: left;
			margin: 30upx 0;
			overflow: hidden;

			image {
				display: block;
				width: 176upx;
				height: 176upx;
				border-radius: 20upx;
			}

			text {
				display: block;
				text-align: center;
				font-size: 30upx;
				color: #e33b2d;
				margin-top: 24upx;
			}
		}
	}
}

.order_finals {
	width: 100%;
	height: 100upx;
	padding: 0 10px;
	background: rgba(255, 255, 255, 1);
	box-shadow: 0px -3px 6px rgba(0, 0, 0, 0.04);
	position: fixed;
	bottom: 0;
	left: 0;
	z-index: 22;
	display: flex;
	justify-content: space-between;
	flex-direction: row-reverse;

	.order_finalsLeft {
		width: 190rpx;
		height: 100rpx;
		overflow: hidden;

		.order_finalBox {
			height: 100rpx;
			overflow: hidden;
			display: flex;
			align-items: center;
			align-items: center;

			.iconfont {
				display: block;
				text-align: center;
				font-size: 42upx;
				color: $color1;
				margin-top: 10upx;
				margin-right: 5px;
			}

			.order_finalPa {
				font-size: 24upx;
				display: block;
				text-align: center;
				margin-top: 5upx;
			}
		}
	}

	.order_finalsRight {
		width: auto;
		height: auto;
		float: right;
		overflow: hidden;

		text {
			width: 130upx;
			height: 56upx;
			background: rgba(255, 255, 255, 1);
			border: 1px solid rgba(191, 191, 191, 1);
			border-radius: 80upx;
			font-size: 24upx;
			line-height: 56upx;
			text-align: center;
			margin-top: 20upx;
			margin-right: 20upx;
			display: inline-block;
		}

		.act {
			background: $color1;
			border: 1px solid $color1;
			color: white;
		}
	}
}

.order_final {
	width: 100%;
	height: 100upx;
	background: rgba(255, 255, 255, 1);
	box-shadow: 0px -3px 6px rgba(0, 0, 0, 0.04);
	position: fixed;
	bottom: 0;
	left: 0;
	z-index: 22;

	.order_finalBox {
		width: 50%;
		height: 100upx;
		float: left;
		position: relative;

		&:nth-child(1):before {
			content: '';
			position: absolute;
			width: 1upx;
			height: 60upx;
			background: #eeeeee;
			right: 0;
			top: 0;
			bottom: 0;
			margin: auto;
		}

		text {
			font-size: 32upx;
			color: #333333;
			line-height: 100upx;
			text-align: center;
			display: block;

			text {
				display: inline-block;
				font-size: 56upx;
				color: #666666;
				margin-right: 24upx;
				vertical-align: middle;
			}
		}
	}
}

.order_main {
	width: 100%;
	height: auto;
	overflow: hidden;
	padding: 0 30rpx 140rpx 30rpx;
	background: white;
	margin-top: 5upx;

	.order_mainTit {
		display: block;
		font-size: 32upx;
		line-height: 38upx;
		padding: 20upx 0;
		border-bottom: 1px #eeeeee solid;
	}

	.order_mainBox {
		width: 100%;
		height: auto;
		overflow: hidden;
		margin-top: 30upx;

		.order_mainPa {
			font-size: 24upx;
			color: #999999;
			line-height: 29upx;
			display: block;
			width: 120upx;
			float: left;
		}

		.order_mainPb {
			font-size: 24upx;
			color: #999999;
			line-height: 29upx;
			display: block;
			width: calc(100% - 120upx);
			padding-left: 10upx;
			float: left;
		}
	}
}

.order_cont {
	width: 100%;
	height: auto;
	overflow: hidden;
	margin-top: 5upx;
	padding:20upx 30upx;
	background: white;

	.order_contBox {
		width: 100%;
		height: auto;
		padding: 10upx 0;
		overflow: hidden;

		.order_contPa {
			font-size: 24upx;
			color: #999999;
			line-height: 45upx;
			float: left;
			display: block;
		}

		.order_contPb {
			font-size: 24upx;
			color: #000000;
			line-height: 45upx;
			float: right;
			display: block;
		}
	}

	.order_contFinal {
		width: 100%;
		height: auto;
		padding: 10upx 0;
		overflow: hidden;

		.order_contPa {
			font-size: 28upx;
			color: #333333;
			line-height: 45upx;
			float: left;
			display: block;
		}

		.order_contPb {
			font-size: 32upx;
			color: $color1;
			line-height: 45upx;
			font-weight: bold;
			float: right;
			display: block;
		}
	}
}

.order_good {
	width: 100%;
	height: 250upx;
	overflow: hidden;
	background: white;
	padding: 0 30upx;

	.order_shop {
		width: 100%;
		height: auto;
		border-bottom: 1px #eeeeee solid;
		padding: 40upx 0 26upx;
		overflow: hidden;

		image {
			display: block;
			width: 40upx;
			height: 38upx;
			float: left;
		}

		text {
			font-size: 28upx;
			color: #333333;
			line-height: 38upx;
			display: block;
			float: left;
			margin-left: 16upx;
		}
	}

	.order_list {
		width: 100%;
		height: auto;
		margin-top: 30upx;
		display: flex;
		justify-content: space-between;
		padding-bottom: 20upx;
		border-bottom: 1px solid #f2f2f2;

		.info {
			margin-right: 10upx;
			display: flex;
			justify-content: flex-start;
			flex: 1 1 auto;
		}

		image {
			display: block;
			width: 200upx;
			height: 200upx;
			flex: 0 0 auto;
		}

		.order_goodBox {
			display: flex;
			height: 200upx;
			padding-left: 20upx;
			flex-wrap: wrap;
			flex: 1;
			align-content: space-between;
			.order_goodName {
				width: 100%;
				height: 88upx;
				font-size: 24upx;
				line-height: 44upx;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
			}

			.order_goodAttr {
				font-size: 24upx;
				color: #999999;
				line-height: 40rpx;
				justify-self: flex-start;
			}
		}

		.order_price {
			display: flex;
			width: 100%;
			justify-content: space-between;
			align-items: center;
			.order_jq {
				font-size: 32upx;
				font-weight: bold;
				line-height: 45upx;
				color: $color1;
			}

			.order_num {
				font-size: 24upx;
				color: #999999;
			}
		}
	}

	.order_btn {
		overflow: hidden;
		float: right;
		width: auto;
		margin-top: 30upx;
		padding-bottom: 30upx;

		text.act {
			border: 1px solid $color1;
			color: #fff;
			background: $color1;
		}

		text {
			display: block;
			width: 130upx;
			height: 48upx;
			background: rgba(255, 255, 255, 1);
			border: 1px solid rgba(191, 191, 191, 1);
			border-radius: 30upx;
			line-height: 48upx;
			color: #333333;
			text-align: center;
			font-size: 24upx;
			float: right;
			margin-left: 30upx;
		}
	}
}

.order_addr {
	width: 100%;
	height: auto;
	padding: 40upx 30upx 20upx 30upx;
	background: white;
	position: relative;
	overflow: hidden;

	&:before {
		content: '';
		position: absolute;
		height: 1px;
		background: #eeeeee;
		left: 30upx;
		right: 30upx;
		bottom: 0;
		transform: scaleY(0.5);
	}

	.iconfont {
		color: #999999;
		font-size: 36upx;
		float: left;
		display: block;
	}

	.order_addrBox {
		width: calc(100% - 50upx);
		height: auto;
		float: left;
		overflow: hidden;
		padding-left: 20upx;

		.order_name {
			font-size: 28upx;
			display: block;
			color: #000000;
			line-height: 40upx;

			text {
				margin-left: 24upx;
				font-size: 28upx;
				color: #333333;
			}
		}

		.order_dizhi {
			display: block;
			font-size: 28upx;
			color: #999999;
			line-height: 34upx;
			margin-top: 11upx;
		}
	}
}

.order_tit {
	width: 100%;
	height: auto;
	overflow: hidden;
	padding: 0 30upx;
	background: linear-gradient(90deg, #fdd100 0%, #fa416a 100%);

	.yticon,
	.iconfont {
		font-size: 36upx;
		line-height: 104upx;
		display: block;
		float: left;
		color: white;
	}

	.order_yy {
		font-size: 32upx;
		margin-left: 20upx;
		line-height: 104upx;
		color: white;
		display: block;
		float: left;
	}

	.tui-countdown {
		font-size: 28upx;
		line-height: 104upx;
		float: right;

		.time {
			color: #fff;
			margin-right: 16upx;
		}
	}
}

.tui-popup-box {
	padding: 40rpx;

	.label {
		line-height: 40rpx;
		font-size: 32rpx;
		color: #333;
	}
	input {
		border: 1px solid #eee;
		border-radius: 5px;
		margin: 15px 0;
		line-height: 68rpx;
		height: 68rpx;
		font-size: 13px;
	}
	.button_wrap {
		display: flex;
		align-items: center;
		justify-content: space-around;
	}
	button {
		width: 260rpx;
	}
}
</style>
